<?

class CLHandlers{

  /**
   * insert into Mutation Invoice
   *
   * @param Database  $Db
   * @param string    $whose
   * @param string    $values
   * @param string    $srcDb
   */
  function insToMutInvoice(&$Db,
                            $whose,
                           &$values,
                            $srcDb){

    $query  = "INSERT INTO ".$srcDb."
               (PERIODE,
                ".$whose.",
                REFERENCE,
                SOURCE,
                DATE_SOURCE,
                TR_DATE,
                DUE_DATE,
                TX_TYPE,
                AMOUNT,
                NO)
               VALUES (".$values.")";

    $Db->execQuery($query);
  }

  /**
   * tansfer Dp real to Tmp table
   *
   * @param Database  $Db
   * @param string    $jobId
   * @param string    $tmpJid
   * @param Composer  $SourceDb
   */
  function transDpRealToTmp(&$Db,
                             $jobId,
                             $tmpJid,
                            &$SourceDb){

    /**
     * transfer...
     */
    $query  = "INSERT INTO ".$SourceDb->fetchValue("srcDbTmpDtlDp")."
               SELECT *
               FROM ".$SourceDb->fetchValue("srcDbDtlDp")."
               WHERE  JOB_ID='".$jobId."' ";

    $Db->execQuery($query);

    $query  = "INSERT INTO ".$SourceDb->fetchValue("srcDbTmpSubDp")."
               SELECT *
               FROM ".$SourceDb->fetchValue("srcDbSubDp")."
               WHERE JOB_ID='".$jobId."' ";

    $Db->execQuery($query);

    /**
     * we reset tmp jobId
     * jobId --> tmpJid
     */
    $query  = "UPDATE ".$SourceDb->fetchValue("srcDbTmpDtlDp")."
               SET JOB_ID='".$tmpJid."'
               WHERE  JOB_ID='".$jobId."' ";

    $Db->execQuery($query);

    $query  = "UPDATE ".$SourceDb->fetchValue("srcDbTmpSubDp")."
               SET JOB_ID='".$tmpJid."'
               WHERE  JOB_ID='".$jobId."' ";

    $Db->execQuery($query);
  }

  /**
   * tansfer the tmp to real table
   *
   * @param Database  $Db
   * @param string    $jobId
   * @param string    $tmpJid
   * @param Composer  $SourceDb
   */
  function transDpTmpToReal(&$Db,
                             $jobId,
                             $tmpJid,
                            &$SourceDb){

    /**
     * transfer...
     */
    $query  = "INSERT INTO ".$SourceDb->fetchValue("srcDbDtlDp")."
               SELECT *
               FROM ".$SourceDb->fetchValue("srcDbTmpDtlDp")."
               WHERE  JOB_ID='".$tmpJid."' ";

    $Db->execQuery($query);

    $query  = "INSERT INTO ".$SourceDb->fetchValue("srcDbSubDp")."
               SELECT *
               FROM ".$SourceDb->fetchValue("srcDbTmpSubDp")."
               WHERE JOB_ID='".$tmpJid."' ";

    $Db->execQuery($query);

    /**
     * we set the Jobid
     * tmpJid --> jobId
     */
    $query  = "UPDATE ".$SourceDb->fetchValue("srcDbDtlDp")."
               SET JOB_ID='".$jobId."'
               WHERE  JOB_ID='".$tmpJid."' ";

    $Db->execQuery($query);

    $query  = "UPDATE ".$SourceDb->fetchValue("srcDbSubDp")."
               SET JOB_ID='".$jobId."'
               WHERE  JOB_ID='".$tmpJid."' ";

    $Db->execQuery($query);
  }

  /**
   * insert into detail of DP
   *
   * @param Database  $Db
   * @param string    $values
   * @param string    $srcDb
   */
  function insToDtlDp($Db,
                      $values,
                      $srcDb){

    $query  = "INSERT INTO ".$srcDb."
               (JOB_ID,
                TOT_INVOICE,
                PAYMENT,
                GAIN_LOSS,
                DISCOUNT)
               VALUES (".$values.")";

    $Db->execQuery($query);
  }

  /**
   * insert into sub detail of DP
   *
   * @param Database  $Db
   * @param string    $values
   * @param string    $srcDb
   */
  function insToSubDp($Db,
                      $values,
                      $srcDb){

    $query  = "INSERT INTO ".$srcDb."
               (JOB_ID,
                NO,
                PERIODE,
                REFERENCE,
                TR_DATE,
                DUE_DATE,
                SUPPLIER_CUSTOMER,
                CURRENCY,
                RATE,
                REMAIN,
                AMOUNT,
                TX_TYPE)
               VALUES (".$values.")";

    $Db->execQuery($query);
  }

  /**
  * insert into detail
  *
  * @param  string  $COracle
  * @param  string  $values
  * @param  string  $srcDb
  *
  */
  function insToDbSubDtl(&$COracle,
                         &$values,
                         &$srcDb){

    $query  = "INSERT INTO ".$srcDb."
               (JOB_ID,
                WAREHOUSE,
                MODEL,
                MODEL_ID,
                CVT,
                CVT_QTY,
                UNIT,
                QTY,
                PRICE,
                HRZ_TOT,
                DISC_PCTG,
                DISC,
                NET,
                NO)
               VALUES (".$values.") ";

    $COracle->execQuery($query);
  }

  /**
  * insert into model input history
  *
  * @param  string  $COracle
  * @param  string  $values
  * @param  string  $srcDb
  *
  */
  function insTxHistory(&$COracle,
                        &$values,
                        &$srcDb){

    $query  = "INSERT INTO ".$srcDb."
               (JOB_ID,
                TR_DATE,
                MODEL_ID,
                QTY,
                PRICE,
                NET,
                NO,
                TX_TYPE)
               VALUES (".$values.") ";

    $COracle->execQuery($query);
  }

  /**
  * insert into detail
  *
  * @param  string  $COracle
  * @param  string  $values
  * @param  string  $srcDb
  *
  */
  function insToDbDtl(&$COracle,
                      &$values,
                      &$srcDb){

    $query  = "INSERT INTO ".$srcDb."
               (JOB_ID,
                TOT_HRZ_TOT,
                TOT_DISC,
                TOT_NET)
               VALUES (".$values.") ";

    $COracle->execQuery($query);
  }

  /**
  * insert into header
  *
  * @param  string  $COracle
  * @param  string  $values
  * @param  string  $srcDb
  *
  */
  function insToDbHdr(&$COracle,
                      &$values,
                      &$srcDb){

    $query  = "INSERT INTO ".$srcDb."
               (JOB_ID,
                REFERENCE,
                SUPPLIER_NAME,
                SUPPLIER_CODE,
                TR_DATE,
                DUE_DATE,
                CURRENCY,
                RATE,
                CREDIT_TERM,
                NOTA_SRC,
                AMOUNT,
                TAX_PCTG,
                TAX,
                DISC_PCTG,
                DISC,
                EXPENSE,
                PREPAID,
                PAYABLE,
                MEMO,
                NO,
                STATUS)
               VALUES (".$values.") ";

    $COracle->execQuery($query);
  }

  /**
  * insert the Account payable DB
  *
  * @param  string  $COracle
  * @param  string  $jobId
  * @param  string  $prepaid
  * @param  string  $status
  * @param  string  $srcDb
  *
  */
  function insAP(&$COracle,
                 &$jobId,
                 &$srcDate,
                 &$payable,
                 &$status,
                 &$srcDb){

    $srcDbSequence      = "MST_SEQUENCE";

    CGFetchers::fetchHdrNo($COracle,
                           $srcDate,
                           $srcDb,
                           $lastNumber);

    /**
    * Build up this AP JobId
    *
    */
    $thisPeriode    = &$_SESSION["sysPeriode"];

    $seqEditStatus  = false;

    $thisSequence   = "AP_SEQ";

  	while($seqEditStatus == false){

    	CGFetchers::fetchCurSequence($COracle,
                  	               $thisSequence,
                  	               $thisPeriode,
                  	               $srcDbSequence,
                	                 $srcJidIndex);

    	CGHandlers::initCurSequence($COracle,
                  	              $thisSequence,
                  	              $srcJidIndex,
                  	              $thisPeriode,
                  	              $srcDbSequence,
                  	              $seqEditStatus);
    }

    /**
    * Extract the system period
    *
    */
    $tmpDate = explode("-", $thisPeriode);
    $wrkDate = $tmpDate[0]. /* This period month */
               "-".
               $tmpDate[1]; /* This period year */

    $jobIdAP = addDigit((string) $srcJidIndex).
               $srcJidIndex.
               "/" .
               $wrkDate;

    $trDate = " TO_DATE('".$srcDate."', 'dd-mm-YYYY') ";

    $values = "  ".$lastNumber." ,  ".
              " '".$jobIdAP."',     ".
              "  ".$trDate." ,   ".
              " '".$jobId."',       ".
              " '".$status."',      ".
              "  ".$payable."       ";

    $query  = "INSERT INTO ".$srcDb."
               (NO,
                JOB_ID,
                TR_DATE,
                SRC_REF,
                STATUS,
                PAYABLE)
               VALUES (".$values.") ";

    $COracle->execQuery($query);
  }

  /**
  * insert the Down Payment DB
  *
  * @param  string  $COracle
  * @param  string  $jobId
  * @param  string  $srcDate
  * @param  string  $currency
  * @param  integer $rate
  * @param  integer $used
  * @param  string  $prepaid
  * @param  string  $status
  * @param  string  $srcDb
  *
  */
  function insDP(&$COracle,
                 &$jobId,
                 &$srcDate,
                 &$currency,
                 &$rate,
                 &$used,
                 &$prepaid,
                 &$status,
                 &$srcDb){

    $srcDbSequence      = "MST_SEQUENCE";

    CGFetchers::fetchHdrNo($COracle,
                           $srcDate,
                           $srcDb,
                           $lastNumber);

    $thisPeriode    = &$_SESSION["sysPeriode"];

    $trDate = " TO_DATE('".$srcDate."', 'dd-mm-YYYY') ";

    $values = "  ".$lastNumber." ,  ".
              " '".$jobId."',       ".
              "  ".$trDate." ,      ".
              " '".$currency."',    ".
              "  ".$rate." ,        ".
              "  ".$payable."       ".
              "  ".$used."          ".
              " '".$status."'       ";

    $query  = "INSERT INTO ".$srcDb."
               (NO,
                SRC_ID,
                TR_DATE,
                CURRENCY,
                RATE,
                AMOUNT,
                USED,
                TX_TYPE)
               VALUES (".$values.") ";

    $COracle->execQuery($query);
  }

  /**
  * update the account payable entry
  *
  * @param  string  $COracle
  * @param  string  $jobId
  * @param  string  $status
  * @param  string  $srcDb
  *
  */
  function updateAP( &$COracle,
                     &$jobId,
                     &$status,
                     &$srcDb){

    $query  = "UPDATE ".$srcDb."
               SET  STATUS='".$status."'
               WHERE  SRC_REF='".$jobId."' ";

    $COracle->execQuery($query);
  }

  /**
  * update the down payment entry
  *
  * @param  string  $COracle
  * @param  string  $jobId
  * @param  string  $status
  * @param  string  $srcDb
  *
  */
  function updateDP(&$COracle,
                    &$jobId,
                    &$status,
                    &$srcDb){

    $query  = "UPDATE ".$srcDb."
               SET  STATUS='".$status."'
               WHERE  SRC_REF='".$jobId."' ";

    $COracle->execQuery($query);
  }

  /**
  * insert into inventory
  *
  * @param  string  $COracle
  * @param  string  $noSub      source number of sub detail
  * @param  string  $model      model name
  * @param  string  $modelId
  * @param  string  $unit
  * @param  string  $qty
  * @param  string  $srcDb
  *
  */
  function insToInventory(&$COracle,
                          &$noSub,
                          &$model,
                          &$modelId,
                          &$unit,
                          $qty,
                          &$srcDb){

    /**
    * Check if previous entry of this modelId and modelName
    * isExists
    * YES: Add the qty by 1
    * NO : Add new entry of modelId and modelName
    *
    */
    fetchInvModel($COracle,
                  $model,
                  $modelId,
                  $unit,
                  $srcDb,
                  $isExists,
                  $resPrevious);

    if($isExists){

      $qty  +=  $resPrevious["QTY"][0];

      $query  = "UPDATE ".$srcDb."
                 SET  QTY=".$qty."
                 WHERE  MODEL='".$model."'
                        AND MODEL_ID='".$modelId."'
                        AND UNIT='".$unit."' ";
    }else{

      $lastNumber = 1; /* Default 1st number */

      $values = "  ".$lastNumber." , ".
                " '".$model."',      ".
                "  ".$modelId." ,    ".
                " '".$unit."',       ".
                "  ".$qty."          ";

      $query  = "INSERT INTO ".$srcDb."
                 (NO,
                  MODEL,
                  MODEL_ID,
                  UNIT,
                  QTY)
                 VALUES (".$values.") ";

    }


    $COracle->execQuery($query);
  }

  /**
  * insert into this model name and model id history
  *
  * @param  string  $COracle
  * @param  string  $values
  * @param  integer $txType
  * @param  string  $srcDb
  *
  */
  function insToHistory( &$COracle,
                         &$values,
                         &$txType,
                         &$srcDb){

    $values .= ",".$txType." ";

    $query  = "INSERT INTO ".$srcDb."
               (NO,
                TR_DATE,
                SRC_REF,
                SRC_NO,
                MODEL,
                MODEL_ID,
                UNIT,
                SER_NUMBER,
                TX_TYPE)
               VALUES (".$values.") ";

    $COracle->execQuery($query);
  }

  /**
  * Delete model entry from model history
  *
  * @param  string  $COracle
  * @param  string  $recvId
  * @param  integer $noDetail
  * @param  string  $model
  * @param  integer $modelId
  * @param  string  $unit
  * @param  string  $serNumber
  * @param  string  $srcDb
  *
  */
  function delModelEntry(&$COracle,
                         &$recvId,
                         &$noDetail,
                         &$model,
                         &$modelId,
                         &$unit,
                         &$serNumber,
                         &$srcDb){

    checkCleanModel($COracle,
                    $recvId,
                    $noDetail,
                    $model,
                    $modelId,
                    $unit,
                    $serNumber,
                    $srcDb,
                    $isClean);

    $query  = "DELETE FROM ".$srcDb."
               WHERE  SRC_REF='".$recvId."'
                      AND SRC_NO=".$noDetail."
                      AND MODEL='".$model."'
                      AND MODEL_ID='".$modelId."'
                      AND UNIT='".$unit."'
                      AND SER_NUMBER='".$serNumber."' ";

    $COracle->execQuery($query);
  }

  /**
  * decrease inventory balance
  *
  * @param  string  $COracle
  * @param  string  $model
  * @param  integer $modelId
  * @param  string  $unit
  * @param  string  $defDecrement
  * @param  integer $srcDb
  *
  */
  function decrInventory(&$COracle,
                         &$model,
                         &$modelId,
                         &$unit,
                         $defDecrement,
                         &$srcDb){

    fetchInvBalance($COracle,
                    $model,
                    $modelId,
                    $unit,
                    $srcDb,
                    $qty);

    if(!$qty){

      usrDie("Error: Data corrupted, Invalid balance");
    }else{

      $qty -= $defDecrement;
    }

    $query = "UPDATE ".$srcDb."
              SET QTY=".$qty."
              WHERE MODEL='".$model."'
                    AND MODEL_ID='".$modelId."'
                    AND UNIT='".$unit."' ";

    $COracle->execQuery($query);
  }

  /**
  * update the model's input history
  *
  * @param  string  $COracle
  * @param  string  $baseStatus
  * @param  string  $txStatus
  * @param  string  $jobId
  * @param  integer $txType
  * @param  string  $srcDb
  *
  */
  function updateInputHistory(&$COracle,
                              &$baseStatus,
                              &$txStatus,
                              &$jobId,
                              $txType,
                              &$srcDb){

    $query  = "UPDATE ".$srcDb."
               SET  STATUS='".$txStatus."'
               WHERE  JOB_ID='".$jobId."'
                      AND STATUS='".$baseStatus."'
                      AND TX_TYPE=".$txType." ";

    $COracle->execQuery($query);
  }

  /**
  * Insert the header of this manual serial number input
  *
  * @param  string  $COracle
  * @param  string  $thisJid
  * @param  string  $srcJid
  * @param  integer $srcDtlNo
  * @param  integer $srcSubNo
  * @param  string  $srcDb
  *
  */
  function insMiniInputHdr( &$COracle,
                            &$thisJid,
                            &$srcJid,
                            &$srcDtlNo,
                            &$srcSubNo,
                            &$srcDb){

    $values = " '".$thisJid."',   ".
              " '".$srcJid."',    ".
              "  ".$srcDtlNo." ,  ".
              "  ".$srcSubNo."    ";

    $query  = "INSERT INTO ".$srcDb."
               (JOB_ID,
                SRC_REF,
                DTL_NO,
                SUB_NO)
               VALUES (".$values.") ";

    $COracle->execQuery($query);
  }

  /**
  * Insert the header of this manual serial number input
  *
  * @param  string  $COracle
  * @param  string  $thisJid
  * @param  string  $serNumber
  * @param  integer $srcDb
  *
  */
  function insMiniInputDtl( &$COracle,
                            &$thisJobId,
                            $serNumber,
                            &$srcDb){

    CGFetchers::fetchLastNumberByJobId($COracle,
                                       $thisJobId,
                                       $srcDb,
                                       $lastNumber);

    $values = " '".$thisJobId."', ".
              " '".$serNumber."', ".
              "  ".$lastNumber."  ";

    $query  = "INSERT INTO ".$srcDb."
               (JOB_ID,
                SER_NUMBER,
                NO)
               VALUES (".$values.") ";

    $COracle->execQuery($query);
  }

  /**
  * update the manual input SRC_REF
  * since the previous is Invalid
  *
  * @param  string  $COracle
  * @param  string  $serNumber
  * @param  string  $jobId
  * @param  string  $srcDb
  *
  */
  function updateManInSrcRef( &$COracle,
                              &$serNumber,
                              &$jobId,
                              &$srcDb){

    /**
    * make sure it is _MANUAL_KEY_IN
    * YES: process the update with the $theSerial[_2ND] as the
    *      manual key in JobId
    * NO : Do nothing
    *
    */
    $theSerial = explode(_DEFAULT_SEPARATOR_02,
                         $serNumber);

    if($theSerial[_1ST] == _MANUAL_KEY_IN){

      $query  = "UPDATE ".$srcDb."
                 SET SRC_REF='".$jobId."'
                 WHERE JOB_ID='".$theSerial[_2ND]."' ";

      $COracle->execQuery($query);
    }else{

      /* Do nothing */
    }
  }
}
?>